function rendergraph() {
    // 列表数据
    let productListStr = localStorage.getItem('LIST')
    let productList = JSON.parse(productListStr) || []
    // console.log(productList)
    // 全选
    
    let stateAllStr = localStorage.getItem('STATEALL')
    let stateAll = JSON.parse(stateAllStr) || false
    // 渲染页面
    let data = { productList, stateAll }
    $('.main').html(template('productList', data))
    //全选事件处理
    $('.allCheck').on('click', function () {
        //  获取全选状态
        let stateAllStr = localStorage.getItem('STATEALL')
        let stateAll = JSON.parse(stateAllStr) || false
        if (stateAll == false) {
            stateAll = true
        } else {
            stateAll = false
        }
        // 获取所有复选框,根据全选状态改变复选状态值
        let productListStr = localStorage.getItem('LIST')
        let productList = JSON.parse(productListStr) || []

        productList.forEach((item) => {
            if (stateAll) {
                item.state = true
            } else {
                item.state = false
            }
        })
        localStorage.setItem('LIST', JSON.stringify(productList))
        localStorage.setItem('STATEALL', stateAll)
        // 重新加载购物车列表
        rendergraph()
    })
    totalPrice() 
}
// 单选
function single(index) {
    // 获取列表和全选数据
    let productListStr = localStorage.getItem('LIST')
    let productList = JSON.parse(productListStr) || []
    let stateAllStr = localStorage.getItem('STATEALL')
    let stateAll = JSON.parse(stateAllStr) || false
    if (productList[index].state) {
        productList[index].state = false
    } else {
        productList[index].state = true
    }
    // 判断长度
    let item=productList.filter((item)=>{return item.state==true})
    if(item.length==productList.length){
        stateAll = true
    }else{
        stateAll = false
    }
    localStorage.setItem('LIST', JSON.stringify(productList))
    localStorage.setItem('STATEALL', stateAll)
    rendergraph()
}

// 加减功能
function minus(index) {
    let productListStr = localStorage.getItem('LIST')
    let productList = JSON.parse(productListStr) || []
    productList[index].num--
    if (productList[index].num <= 0) {
        productList[index].num = 0
    }
    localStorage.setItem('LIST', JSON.stringify(productList))
    rendergraph()
}
function plus(index) {
    let productListStr = localStorage.getItem('LIST')
    let productList = JSON.parse(productListStr) || []
    productList[index].num++
    localStorage.setItem('LIST', JSON.stringify(productList))
    rendergraph()
}
// 删除功能
function deletes(index) {
    let productListStr = localStorage.getItem('LIST')
    let productList = JSON.parse(productListStr) || []
    productList.splice(index, 1)
    localStorage.setItem('LIST', JSON.stringify(productList))
    rendergraph()
}
// 失焦
function blurs(index) {
    let productListStr = localStorage.getItem('LIST')
    let productList = JSON.parse(productListStr) || []
    let num = $('.number1')[index]
    if(num.value<=0){
        num.value=0
    }
    productList[index].num = num.value
    localStorage.setItem('LIST', JSON.stringify(productList))
    rendergraph()
}
// 计算所有选中商品总和
function totalPrice() {
    let productListStr = localStorage.getItem('LIST')
    let productList = JSON.parse(productListStr) || []
    let total=0
    // console.log(productList)
    let item=productList.filter(function(item){return item.state==true})
    // console.log(item);
    item.forEach(function(item){
        total+=item.num*item.purchasePrice
    })
    $('.total').html(total.toFixed(2))
}
// 去首页
$('.homePage').on('click', function () {
    location.href = 'index.html'
})
// 结算
$('.clearing').on('click', function () {
    alert("恭喜你！免单")
})
// 详情页
$('.details').on('click', function () {
    history.go(-1)
})
// 登录页
$(".me").on('click', function () {
    location.href = 'logIn.html'
})
// 客服
$('.details').on('click',function(){
    alert('不好意思，客户大叔去上厕所了')
})
rendergraph()
